package com.union.core.organization.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.union.core.organization.dao.OrganizationManagerDao;
import com.union.core.organization.dao.impl.OrganizationManagerDaoImpl;
import com.union.core.organization.model.OrganizationManager;
import com.union.core.sys.service.SysLogsService;

public class OrganizationManagerService{
	private static Logger log = Logger.getLogger(SysLogsService.class.getName());
	private OrganizationManagerDao organizationManagerDaoImpl;
	
	public OrganizationManagerService(){
		organizationManagerDaoImpl = new OrganizationManagerDaoImpl();
	}
	
	/**
	 * 查询机构信息列表
	 */
	public List<OrganizationManager> query(int pid,int start,int pageSize){
		List<OrganizationManager> OrganizationList = organizationManagerDaoImpl.query(pid, start, pageSize);
		log.info("PLAT: 机构列表: pid="+pid);
		return OrganizationList;
	}
	
	public List<OrganizationManager> queryList(int pid){
		List<OrganizationManager> OrganizationList = organizationManagerDaoImpl.queryList(pid);
		log.info("PLAT: 查询机构("+pid+")的全部机构列表");
		return OrganizationList;
	}
	
	public List<OrganizationManager> queryList(){
		List<OrganizationManager> OrganizationList = organizationManagerDaoImpl.queryList();
		log.info("PLAT: 查询机构的全部机构列表");
		return OrganizationList;
	}
	
	public List<OrganizationManager> queryList(String organization_parent){
		List<OrganizationManager> OrganizationList = organizationManagerDaoImpl.queryList(organization_parent);
		log.info("PLAT: 查询机构("+organization_parent+")的全部机构列表");
		return OrganizationList;
	}
	/**
	 * 查询机构数目
	 */
	public int ListCount(int pid){
		int count = organizationManagerDaoImpl.ListCount(pid);
		log.info("PLAT: 机构数目: count="+count+",pid="+pid);
		return count;
	}
	
	/**
	 * 查询单个机构
	 * @param pid
	 * @param organization_name
	 * @return
	 */
	public OrganizationManager query(int pid,String organization_name){
		OrganizationManager organization = organizationManagerDaoImpl.query(pid, organization_name);
		log.info("PLAT: 机构列表: pid="+pid+",organization_name="+organization_name);
		return organization;
	}
	public OrganizationManager query1(int manager_id){
		OrganizationManager organization = organizationManagerDaoImpl.query1(manager_id);
		log.info("PLAT: 机构列表: manager_id="+manager_id);
		return organization;
	}
	/**
	 * 查询单个机构
	 * @param organization_id
	 * @return
	 */
	
	public OrganizationManager query(int organization_id){
		OrganizationManager organization = organizationManagerDaoImpl.query(organization_id);
		log.info("PLAT: 机构列表: organization_id="+organization_id);
		return organization;
	}
	
	public OrganizationManager query(String organization_name){
		OrganizationManager organization = organizationManagerDaoImpl.query(organization_name);
		log.info("PLAT: 机构列表: organization_name="+organization_name);
		return organization;
	}
	
	/**
	 * @param organization
	 * @return
	 * 添加机构
	 */
	public void add(OrganizationManager organization){
		organizationManagerDaoImpl.add(organization);
		log.info("PLAT: 添加机构: "+organization.getOrganization_id()+","+organization.getOrganization_name());
	}
	
	/**
	 * 修改机构信息
	 * @param organization
	 * @return
	 */
	public void edit(OrganizationManager organization){
		organizationManagerDaoImpl.edit(organization);
		log.info("PLAT: 修改机构: "+organization.getOrganization_id()+","+organization.getOrganization_name());
	}
	
	/**
	 * 删除机构
	 * @param organization_id
	 * @return
	 */
	public void delete(int organization_id){
		organizationManagerDaoImpl.delete(organization_id);
		log.info("PLAT: 修改机构: organization_id="+organization_id);
	}
	
	public boolean checkPid(int pid){
		log.info("PLAT: 校验是否存在下属机构: "+pid);
		return organizationManagerDaoImpl.checkPid(pid);
	}
	
	public boolean checkOrganizationName(String organization_name){
		log.info("PLAT: 校验是否存在下属机构: "+organization_name);
		return organizationManagerDaoImpl.checkOrganizationName(organization_name);
	}

	
	/**
	 * 根据当前登录人的组织id查询
	 * */
	public List<OrganizationManager> queryChildren(int pid){
		List<OrganizationManager> sorglist=null;
		List<OrganizationManager> torglist=null;
		List<OrganizationManager> forglist=organizationManagerDaoImpl.queryList(pid);
		if(forglist.size()!=0){
			for(OrganizationManager forg:forglist){
				 sorglist=organizationManagerDaoImpl.queryList(forg.getOrganization_id());
				if(sorglist.size()!=0){
				for(OrganizationManager sorg:sorglist){
					torglist=organizationManagerDaoImpl.queryList(sorg.getOrganization_id());
					if(torglist.size()!=0){
						sorg.setChildOrgList(torglist);
					}
				}
				forg.setChildOrgList(sorglist);
				}
			}
		}
		return forglist;
	}
	/**
	 * 根据当前登录人的组织父id和名字查询
	 */
	public List<OrganizationManager> queryparentid(int Organization_id){
		List<OrganizationManager> onelist = null;
		List<OrganizationManager> torgglist = null;
		List<OrganizationManager> threelist = organizationManagerDaoImpl.queryList1(Organization_id);
		System.out.println("Organization_id="+Organization_id);
		if(threelist.size()!=0){
		   for(OrganizationManager three: threelist){
			   onelist =  organizationManagerDaoImpl.queryList1(three.getPid());
			   System.out.println("three="+three.getPid());
			   if(onelist.size()!=0){
				   for(OrganizationManager one : onelist){
					   torgglist = organizationManagerDaoImpl.queryList1(one.getPid());
					   System.out.println("one="+one.getPid());
					   if(torgglist.size()!=0){
						   one.setChildOrgList(torgglist);
					   }
				   }
				  		three.setChildOrgList(onelist);
			   }
		   }	
		}
						return  threelist;
	}

		
}

